import { Application, ILifeCycle, pinus } from "pinus";
import aviator from "../../games/aviator";
import controlConfig from "../../games/aviator/controlConfig";
import gameConfig from "../../games/aviator/gameConfig";


export default function () {
    return new Lifecycle();
}

class Lifecycle implements ILifeCycle {
    beforeStartup(app: Application, cb: () => void): void {
        console.log(app.curServer.id, 'beforeStartup');
        cb();
    }
    afterStartup(app: Application, cb: () => void) {
        // do some operations after application start up
        console.log(app.curServer.id, 'afterStartup');
        cb();
    }

    beforeShutdown(app: Application, shutDown: () => void, cancelShutDownTimer: () => void) {
        console.log(app.getServerId(), '!!beforeShutdown');
        const _aviator = aviator(app)
        _aviator.forceKickAll().then(() => {
            shutDown();
        })
    }

    afterStartAll(app: Application) {
        console.log(app.curServer.id, 'afterStartAll');
        console.log('loading config&control')
        let task = [gameConfig(app).load(), controlConfig(app).load()]
        Promise.all(task).then(() => {
            console.log('loading config&control success')
            //启动游戏  生命周期结束   这里是小飞机的逻辑，需要服务端跑，扫雷暂时不需要
            // aviator(app).run()
        })
    }
}
